package pers.wwz.study.springbootparams.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.UUID;

/**
 * 注意此处只能用@Controller
 */
@Slf4j
@Controller
@RequestMapping("/flash")
public class FlashController {

    /**
     * 执行跳转，并设置传值
     *
     * @param counter
     * @param response
     * @return
     */
    @GetMapping("/first")
    public String first(final RedirectAttributes redirectAttrs) {
        log.info("redirect start:{}");
        redirectAttrs.addFlashAttribute("flash", UUID.randomUUID().toString());
        return "redirect:/flash/second";
    }

    /**
     * 获取传值
     *
     * @param session
     * @param response
     * @return
     */
    @GetMapping("/second")
    @ResponseBody
    public String second(@ModelAttribute("flash") String flash) {
        log.info("redirect receive {}", flash);
        return flash;
    }

}
